package com.goldsprite.gdxcore.entities;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.viewport.*;
import com.goldsprite.gdxcore.utils.*;

import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.goldsprite.gdxcore.logs.LogViewerService;
import com.goldsprite.gdxcore.screens.GScreen;
import com.goldsprite.utils.math.Vector2;

public class GameWorldOld extends WidgetGroup {
	private final Array<IRenderable> entities = new Array<>();
	private float delta;
	private LogViewerService logViewer;
	public GScreen screen;
	public boolean[] isUIEvent = {false};
	public OrthoCamController cameraController;
	public OrthographicCamera worldCam, transCam, uiCamera;
	public float padding = 20;
	public float worldWidth = 1920, worldHeight = 1080;
	public float worldRatio = worldWidth/worldHeight;
	public FrameBuffer fbuf;
	public Texture bufTex;
	public TextureRegion bufRegion;
	public SpriteBatch bufBatch;

	public Texture avatar;
	private Texture blackBackground;

	private Skin skin;
	private Touchpad touchpad;
	public FitViewport viewport;
	public Group uiGroup;
	
	public float initHeight;

	
	public GameWorldOld(Skin skin){
		this.skin = skin;
	}

	public void init(){
		avatar = new Texture("shikeik_avatar.png");
		blackBackground = ColorTextureUtils.createColorTexture(Color.BLACK);

		logViewer = LogViewerService.getInstance();

		worldCam = new OrthographicCamera();
		uiCamera = new OrthographicCamera();
		viewport = new FitViewport(worldWidth, worldHeight, uiCamera);
		{
			uiCamera.viewportWidth = worldWidth;
			uiCamera.viewportHeight = worldHeight;
		}
		transCam = new OrthographicCamera();
		cameraController = new OrthoCamController(transCam, isUIEvent);
		screen.getImp().addProcessor(cameraController);

		fbuf = new FrameBuffer(Pixmap.Format.RGBA8888, (int)worldWidth, (int)worldHeight, false);
		bufTex = fbuf.getColorBufferTexture();
		bufRegion = new TextureRegion(bufTex);
		bufRegion.flip(false, true);
		bufBatch = new SpriteBatch();

		// 创建摇杆
		Touchpad touchpad = new Touchpad(5, skin); // 10 是死区半径，摇杆在此范围内不触发动作
		touchpad.setBounds(0, 0, 150, 150); // 设置位置和大小
		touchpad.addListener(new InputListener() {
				public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
					Vector2 localPos = new Vector2(x, y);
					//screenToLocalCoordinates(localPos);
					LogViewerService.log("触摸板按下: 舞台x: %.1f, 舞台y: %.1f, 转本地xy: %s", x, y, localPos);
					return true;
				}
			});
		addActor(touchpad);
	}

	public void bindScreen(GScreen screen){
		this.screen = screen;
	}

	public void addEntity(IRenderable entity) {
		entities.add(entity);
		//entity.setWorld(this);
	}

	public void removeEntity(IRenderable entity) {
		entities.removeValue(entity, true);
	}

	@Override
	public void act(float delta) {
		super.act(delta);
		//uiGroup.act(delta);

		this.delta = delta;
		updateWorldCamera();
		// 渲染到 FrameBuffer
		{
			Viewport viewport = screen.getViewport();
			fbuf.begin();
			ScreenUtils.clear(0, 0, 0, 0);
			bufBatch.setProjectionMatrix(worldCam.combined);
			bufBatch.begin();
			//bufBatch.setColor(1f, 0f, 0f, 1f);
			drawFBuf(bufBatch);
			bufBatch.end();
			fbuf.end();  // 渲染结束
			viewport.apply(true);
		}
	}

	private void drawFBuf(SpriteBatch bufBatch) {
		for(IRenderable ent : entities){
			ent.render(bufBatch, delta);
		}
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		//Vector2 pos = new Vector2(getX(), getY());
		//pos = localToStageCoordinates(pos);
		//logViewer.log(String.format("stageX: %.1f, stageY: %.1f, width: %.1f, height: %.1f", pos.x, pos.y, getWidth(), getHeight()));

		//应用到布局
		{
			batch.setProjectionMatrix(screen.getCamera().combined);//必须有，被fbuf改了
			float x, y, width, height;
			Group layout = getParent();
			float lx = layout.getX(), ly = layout.getY(), lw = layout.getWidth(), lh = /*initHeight;*/layout.getHeight();
			width = lh * worldRatio;
			height = lh;
			x = lx + lw/2 - width/2;
			y = ly;
			if(lw < width){
				width = lw;
				height = lw / worldRatio;
				x = lx;
				y = ly + lh/2 - height/2;
			}
			realViewRec[0] = x;
			realViewRec[1] = y;
			realViewRec[2] = width;
			realViewRec[3] = height;
			batch.draw(blackBackground, x, y, width, height);//绘制黑色底色
			batch.draw(bufRegion, x, y, width, height);
		}
	}

	public void updateWorldCamera(){
		float width = worldWidth;
		float height = worldHeight;
		float camX = 0;
		float camY = 0;


		camX += transCam.position.x * 1;
		camY += transCam.position.y * 1/worldRatio;
		worldCam.zoom = transCam.zoom;

		worldCam.viewportWidth = width;
		worldCam.viewportHeight = height;
		worldCam.position.set(camX+width/2, camY+height/2, 0);
		worldCam.update();

		transCam.viewportWidth = worldCam.viewportWidth;
		transCam.viewportHeight = worldCam.viewportHeight;
		//transCam.position.set(worldCam.position);
		transCam.update();
	}

	float[] realViewRec = new float[4];
	public float[] getRealViewRec(){
		return realViewRec;
	}
}
